/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.open.api.core.repository.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import info.peigen.hotpot.component.open.api.core.action.OpenApiConvert;
import info.peigen.hotpot.component.open.api.core.repository.entity.OpenApiLogEntity;
import info.peigen.hotpot.component.open.api.core.repository.mapper.OpenApiLogMapper;
import info.peigen.hotpot.component.open.api.facade.Api;
import org.noear.solon.annotation.Component;

/**
 * <b>(OpenApiLogRepository)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/8/5
 */
@Component
public class OpenApiLogRepository extends AbstractBeetlSqlRepository<OpenApiLogMapper, OpenApiLogEntity> {
    public void store(Api api) {
        OpenApiLogEntity newEntity = OpenApiConvert.convert(api);

        OpenApiLogEntity entity = getMapper().findByRequestNo(api.getRequestNo());
        if (ObjectUtil.isNotNull(entity)) {
            BeanUtil.copyProperties(newEntity, entity, new CopyOptions().setIgnoreNullValue(true));
            saveOrUpdate(entity);
            return;
        }

        saveOrUpdate(newEntity);
    }

    public Api active(String requestNo) {
        OpenApiLogEntity entity = getMapper().findByRequestNo(requestNo);
        return OpenApiConvert.convert(entity);
    }
}